package com.example.constellation.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.constellation.Fragment.Fragment1;
import com.example.constellation.Fragment.Fragment2;
import com.example.constellation.Fragment.Fragment3;
import com.example.constellation.Fragment.Fragment4;
import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.utils.AssetsUtils;
import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {
    private RadioGroup rg;
    private Fragment fg1,fg2,fg3,fg4;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg=findViewById(R.id.rg);
        //加载星座相关数据
        StarBean infoBean = loaddata();
        Bundle bundle=new Bundle();
        bundle.putSerializable("info",infoBean);
        fg1=new Fragment1();
        fg1.setArguments(bundle);
        fg2=new Fragment2();
        fg2.setArguments(bundle);
        fg3=new Fragment3();
        fg3.setArguments(bundle);
        fg4=new Fragment4();
        fg4.setArguments(bundle);
        addFragment();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction ft= getSupportFragmentManager().beginTransaction();
                switch (checkedId) {
                    //Fragment replace 替换 或 add/hide/show
                    case R.id.rb1:
                        ft.show(fg1);
                        ft.hide(fg2);
                        ft.hide(fg3);
                        ft.hide(fg4);
                        break;
                    case R.id.rb2:
                        ft.hide(fg1);
                        ft.show(fg2);
                        ft.hide(fg3);
                        ft.hide(fg4);
                        break;
                    case R.id.rb3:
                        ft.hide(fg1);
                        ft.hide(fg2);
                        ft.show(fg3);
                        ft.hide(fg4);
                        break;
                    case R.id.rb4:
                        ft.hide(fg1);
                        ft.hide(fg2);
                        ft.hide(fg3);
                        ft.show(fg4);
                        break;
                }
                ft.commit();
            }
        });
    }
//读取xzcontnent.json文件
    private StarBean loaddata() {
        String json = AssetsUtils.getString(this, "xzcontent/xzcontent.json");
        Gson gson=new Gson();
        //数据解析得到对象
        StarBean info = gson.fromJson(json, StarBean.class);
        //运用对象得到图片
        AssetsUtils.savelogomap(this,info);
        return info;

    }

    //加fragment一起加载有用显示，没用隐藏，
    private void addFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(R.id.fl,fg1);
        ft.add(R.id.fl,fg2);
        ft.add(R.id.fl,fg3);
        ft.add(R.id.fl,fg4);
        ft.hide(fg2);
        ft.hide(fg3);
        ft.hide(fg4);
        ft.commit();
    }
}
